package ut.servlet.member.paypal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class WithdrawMoney extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		// 取得並驗證使用者輸入資訊
		int money;
		String paypalEmail;
		if(request.getParameter("money") == null || request.getParameter("paypal_email") == null){
			return;
		}else{
			try{
				money = Integer.parseInt(request.getParameter("money"));
				if(money < 200 || money > 100000){
					return;
				}
			}catch(NumberFormatException e){
				return;
			}
			paypalEmail = request.getParameter("paypal_email");
			if(!paypalEmail.matches(".+@.+")){
				return;
			}
		}
		// 確認使用者已經完成信箱驗證
		ut.data.User user = (ut.data.User)request.getSession().getAttribute("user");
		if(user.verify == 0){
			return;
		}
		// 更新資料庫
		response.setHeader("result", ut.data.Transaction.withdraw(user, money, paypalEmail) + ""); // true: 成功、false: 失敗
	}
}